package com.ruoyi.course.service.impl;

import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.constant.SysConstants;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.SnowflakeUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.vo.PageResult;
import com.ruoyi.questionBank.domain.Question;
import com.ruoyi.questionBank.service.IQuestionService;
import com.ruoyi.questionBank.vo.QuestionListVO;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.course.mapper.SysCourseMapper;
import com.ruoyi.course.domain.SysCourse;
import com.ruoyi.course.service.ISysCourseService;

/**
 * 课程Service业务层处理
 * 
 * @author YJF
 * @date 2023-05-17
 */
@Service
public class SysCourseServiceImpl implements ISysCourseService 
{
    @Autowired
    private SysCourseMapper sysCourseMapper;
    @Autowired
    private ISysDictTypeService dictTypeService;
    @Autowired
    private IQuestionService questionService;

    @Value("${ruoyi.course.video.upload}")
    private String uploadPath;

    /**
     * 查询课程
     * 
     * @param courseId 课程主键
     * @return 课程
     */
    @Override
    public SysCourse selectSysCourseByCourseId(Long courseId)
    {
        return sysCourseMapper.selectSysCourseByCourseId(courseId);
    }

    /**
     * 查询课程列表
     * 
     * @param sysCourse 课程
     * @return 课程
     */
    @Override
    public List<SysCourse> selectSysCourseList(SysCourse sysCourse)
    {
        if(StringUtils.isNull( sysCourse.getDelFlag())){
            sysCourse.setDelFlag(SysConstants.CONSTANT_STR0);
        }
        return sysCourseMapper.selectSysCourseList(sysCourse);
    }

    /**
     * 查询全部课程列表
     *
     * @param sysCourse 课程
     * @return 课程
     */
    @Override
    public List<SysCourse> selectWholeList(SysCourse sysCourse)
    {
        if(StringUtils.isNull( sysCourse.getDelFlag())){
            sysCourse.setDelFlag(SysConstants.CONSTANT_STR0);
        }
        List<SysCourse> list = sysCourseMapper.selectWholeList(sysCourse);

        List<SysDictData> datas = dictTypeService.selectDictDataByType("sys_course_type");
        Map<Integer ,String> valMap = datas.stream()
                .collect(Collectors.toMap(data -> Integer.parseInt(data.getDictValue()) ,SysDictData::getDictLabel));
        for (SysCourse course : list) {
            String name = valMap.get(course.getCourseType());
            course.setCourseTypeName(name);
        }

        return list;
    }

    /**
     * 新增课程
     * 
     * @param sysCourse 课程
     * @return 结果
     */
    @Override
    public int insertSysCourse(SysCourse sysCourse)
    {
        //编号
        sysCourse.setCourseNo(SnowflakeUtils.snowflakeId());
        Long userId = SecurityUtils.getUserId();
        sysCourse.setUid(userId);
        //发布时间
        if(SysConstants.CONSTANT_INT0.equals(sysCourse.getReleaseStatus())){
            sysCourse.setReleaseTime(DateUtils.getNowDate());
        }
        sysCourse.setCreateTime(DateUtils.getNowDate());
        sysCourse.setUpdateTime(DateUtils.getNowDate());
        return sysCourseMapper.insertSysCourse(sysCourse);
    }

    /**
     * 修改课程
     * 
     * @param sysCourse 课程
     * @return 结果
     */
    @Override
    public int updateSysCourse(SysCourse sysCourse)
    {
        //发布时间
        SysCourse info = sysCourseMapper.selectSysCourseByCourseId(sysCourse.getCourseId());
        if(!sysCourse.getReleaseStatus().equals(info.getReleaseStatus())){
            if(SysConstants.CONSTANT_INT0.equals(sysCourse.getReleaseStatus())){
                sysCourse.setReleaseTime(DateUtils.getNowDate());
            }
        }
        sysCourse.setUpdateTime(DateUtils.getNowDate());
        return sysCourseMapper.updateSysCourse(sysCourse);
    }

    /**
     * 批量删除课程
     * 
     * @param courseIds 需要删除的课程主键
     * @return 结果
     */
    @Override
    public int deleteSysCourseByCourseIds(Long[] courseIds)
    {
        return sysCourseMapper.deleteSysCourseByCourseIds(courseIds);
    }

    /**
     * 删除课程信息
     * 
     * @param courseId 课程主键
     * @return 结果
     */
    @Override
    public int deleteSysCourseByCourseId(Long courseId)
    {
        return sysCourseMapper.deleteSysCourseByCourseId(courseId);
    }

    /**
     * 校验课程名称是否唯一
     * @param sysCourse 课程
     * @return 结果
     */
    @Override
    public String checkCourseNameUnique(SysCourse sysCourse)
    {
        sysCourse.setDelFlag(SysConstants.CONSTANT_STR0);
        int count = sysCourseMapper.checkCourseNameUnique(sysCourse);
        if (count > 0)
        {
            return SysConstants.CONSTANT_STR1;
        }
        return SysConstants.CONSTANT_STR0;
    }

    @Override
    public void uploadVideo(long courseNo , InputStream in) {
        SysCourse course = sysCourseMapper.selectSysCourseByCourseNo (courseNo);
        if(course == null){
            throw new IllegalArgumentException("course [" + courseNo + "] not exist");
        }
        String fn = uploadPath;
        if(!uploadPath.endsWith("/")){
            fn += "/";
        }
        fn += course.getCourseNo() + ".video";
//        String ext = FileUtil.extName(fileName);
//        if(StrUtil.isNotBlank(ext)){
//            fn += "." + ext;
//        }
        FileUtil.writeFromStream(in ,fn);
    }

    @Override
    public String getVideoPath(long courseNo) {
        String fn = uploadPath;
        if(!uploadPath.endsWith("/")){
            fn += "/";
        }
        fn += courseNo + ".video";
        return fn;
    }

    @Override
    public List<Question> getCourseExamQuestion(long courseNo) {
        QuestionListVO listVO = new QuestionListVO();
        listVO.setEnable(true);
        listVO.setPageSize(Integer.MAX_VALUE);
        listVO.setCourseNo(courseNo);
        PageResult<Question> page = questionService.get(listVO);
        return page.getData();
    }
}
